import numpy as np
import sys
from utils.read_info import get_file_arr


"""
hadoop jar /data/cloudera/parcels/CDH/jars/hadoop-streaming-2.6.0-cdh5.13.0.jar 
        -input /tmp/inputFile.txt 
        -output /tmp/python-out 
        -mapper "python3 /home/hdfs/xws/chp15_hadoop_mapper.py" 
        -reducer "python3 /home/hdfs/xws/chp15_hadoop_reducer.py" 
        -file /home/hdfs/xws/chp15_hadoop_mapper.py 
        -file /home/hdfs/xws/chp15_hadoop_reducer.py

"""
def calc_mean_var(input_file):
    """
    计算均值和方差
    :param input_file: 输入文件
    :return:
    """

    input_info = get_file_arr(input_file, delimiter=' ')
    input_info = [list(map(float, info)) for info in input_info]
    input_data = np.mat(input_info)

    total_num_count = input_data[:, 0].sum()
    total_num_sum = np.multiply(input_data[:, 0], input_data[:, 1]).sum()
    total_num_square = np.multiply(input_data[:, 0], input_data[:, 2]).sum()

    total_mean = total_num_sum / total_num_count
    total_var = total_num_square / total_num_count

    print('{} {} {}'.format(total_num_count, total_mean, total_var))


if __name__ == '__main__':
    input_file = sys.stdin
    calc_mean_var(input_file)
